<template>
  <div>
    <!-- 表格 -->
    <el-table style="width: 100%" border :data="SkuList">
      <el-table-column type="index" label="序号" width="80" align="center">
      </el-table-column>
      <el-table-column prop="skuName" label="名称" width="width">
      </el-table-column>
      <el-table-column prop="skuDesc" label="描述" width="width">
      </el-table-column>
      <el-table-column label="默认图片" width="110">
        <template slot-scope="{ row, $index }">
          <img
            :src="row.skuDefaultImg"
            alt=""
            style="width: 80px; height: 80px"
          />
        </template>
      </el-table-column>
      <el-table-column prop="weight" label="重量" width="80"> </el-table-column>
      <el-table-column prop="price" label="价格" width="80"> </el-table-column>
      <el-table-column label="操作" width="width">
        <template slot-scope="{ row, $index }">
          <el-button
            title="下架"
            type="success"
            icon="el-icon-sort-down"
            size="mini"
            v-if="row.isSale == 0"
            @click="DownSale(row)"
          ></el-button>
          <el-button
            title="上架"
            type="success"
            icon="el-icon-sort-up"
            size="mini"
            v-else
            @click="UpSale(row)"
          ></el-button>
          <el-button
            title="修改"
            type="primary"
            icon="el-icon-edit"
            size="mini"
            @click="edit(row)"
          ></el-button>
          <el-button
            title="查看详情"
            type="info"
            icon="el-icon-info"
            size="mini"
            @click="getSkuInfo(row)"
          ></el-button>
          <el-button
            title="删除"
            type="danger"
            icon="el-icon-delete"
            size="mini"
            @click="deletesku(row.id)"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      style="text-align: center"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="page"
      :page-sizes="[3, 5, 10]"
      :page-size="limit"
      layout="prev, pager, next, jumper,->,sizes,total"
      :total="total"
    >
    </el-pagination>
    <!-- 点击修改按钮弹出的对话框 -->
    <el-dialog title="修改" :visible.sync="skuDialogFormVisible">
      <el-form :model="skuform" :rules="skuRules" ref="skuref">
        <el-form-item prop="skuName" label="名称" label-width="width">
          <el-input v-model="skuform.skuName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item prop="skuDesc" label="描述" label-width="Width">
          <el-input v-model="skuform.skuDesc" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item prop="skuDefaultImg" label="默认图片" label-width="Width">
          <el-upload
            class="avatar-uploader"
            action="/dev-api/admin/product/fileUpload"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img
              v-if="skuform.skuDefaultImg"
              :src="skuform.skuDefaultImg"
              class="avatar"
            />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item prop="weight" label="重量" label-width="Width">
          <el-input v-model="skuform.weight" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item prop="price" label="价格" label-width="Width">
          <el-input v-model="skuform.price" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="skuDialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="skuDialogFormVisible = false"
          >确 定</el-button
        >
      </div>
    </el-dialog>
    <!-- 点击查看详情滑出抽屉效果 -->
    <el-drawer :visible.sync="drawer" size="50%" :show-close="false">
      <el-row :gutter="20">
        <el-col :span="5" style="text-align: right; margin-bottom: 80px"
          >名称</el-col
        >
        <el-col :span="16">{{ skuInfo.skuName }}</el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="5" style="text-align: right; margin-bottom: 80px"
          >描述</el-col
        >
        <el-col :span="16">{{ skuInfo.skuDesc }}</el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="5" style="text-align: right; margin-bottom: 80px"
          >价格</el-col
        >
        <el-col :span="16">{{ skuInfo.price }}</el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="5" style="text-align: right; margin-bottom: 80px"
          >平台属性</el-col
        >
        <el-col :span="16">
          <template>
            <el-tag
              type="success"
              style="margin-right: 10px"
              v-for="item in skuInfo.skuAttrValueList"
              :key="item.id"
              >{{ item.attrId }}-{{ item.valueId }}</el-tag
            >
          </template>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="5" style="text-align: right; margin-bottom: 80px"
          >商品图片</el-col
        >
        <el-col :span="16">
          <template>
            <!-- Carousel 走马灯 -->
            <el-carousel height="150px">
              <el-carousel-item
                v-for="item in skuInfo.skuImageList"
                :key="item.id"
              >
                <img :src="item.imgUrl" alt="" />
              </el-carousel-item>
            </el-carousel>
          </template>
        </el-col>
      </el-row>
    </el-drawer>
  </div>
</template>
<script>
export default {
  name: "Sku",
  data() {
    return {
      page: 1, //默认展示第一页
      limit: 10, //默认每页展示多少条数据
      SkuList: [], //存放skus表格数据
      total: 0, //存放总的数据条数
      skuInfo: {}, //存放详情数据
      drawer: false, //控制抽屉的显示与隐藏
      skuform: {
        skuName: "",
        skuDesc: "",
        skuDefaultImg: "",
        weight: "",
        price: "",
      }, //存储sku数据
      skuDialogFormVisible: false, //控制修改对话框的显示与隐藏
      //表单的验证规则对象
      skuRules: {
        skuName: [
          {
            required: true,
            message: "请输入名称",
            trigger: "blur",
          },
        ],
        skuDesc: [
          {
            required: true,
            message: "请输入具体描述",
            trigger: "blur",
          },
        ],
        skuDefaultImg: [
          {
            required: true,
            message: "请选择默认图片",
            trigger: "change",
          },
        ],
        weight: [
          {
            required: true,
            message: "请输入重量",
            trigger: "blur",
          },
        ],
        price: [
          {
            required: true,
            message: "请输入价格",
            trigger: "blur",
          },
        ],
      },
    };
  },
  //组件挂载完毕
  mounted() {
    this.getSkuList();
  },
  methods: {
    //每页展示多少条数据改变触发的事件
    handleSizeChange(limit) {
      this.limit = limit;
      this.getSkuList();
    },
    //当前是第几页改变触发的事件
    handleCurrentChange(page) {
      this.page = page;
      this.getSkuList();
    },
    //获取sku表格数据
    async getSkuList() {
      //解构出默认的参数
      const { page, limit } = this;
      //正式发起请求
      const result = await this.$API.sku.reqSkuList(page, limit);
      console.log(result, "sku表格数据");
      if (result.code == 200) {
        this.SkuList = result.data.records;
        this.total = result.data.total;
      }
    },
    //点击上架按钮触发的函数
    async UpSale(row) {
      const result1 = await this.$API.sku.reqUpSale(row.id);
      console.log(result1, "上架");
      if (result1.code == 200) {
        this.$message.success("上架成功");
        row.isSale = 1;
      }
    },
    //点击下架按钮触发的函数
    async DownSale(row) {
      const result2 = await this.$API.sku.reqDownSale(row.id);
      console.log(result2, "下架");
      if (result2.code == 200) {
        this.$message.success("下架成功");
        row.isSale = 0;
      }
    },
    //点击编辑按钮触发的事件
    edit(row) {
      console.log(row, "112233");
      this.skuDialogFormVisible = true;
      this.skuform = row;
    },
    //点击查看详情触发的函数
    async getSkuInfo(row) {
      //当前查看详情按钮后展示抽屉效果
      this.drawer = true;
      const result = await this.$API.sku.reqSkuById(row.id);
      console.log(result, "详情数据");
      if (result.code == 200) {
        this.skuInfo = result.data;
      }
    },
    //图片上传成功之后触发的事件
    handleAvatarSuccess() {
      console.log(res, "22222");
      //把图片真实的url地址给表单数据logoUrl
      this.skuform.skuDefaultImg = res.data;
    },
    //图片上传之前触发的事件
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
    //点击删除按钮触发的事件
    async deletesku(id) {
      console.log(111);
      const result = await this.$confirm(
        "此操作将永久删除该文件, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).catch((error) => {
        return error;
      });
      console.log(result, "44444");
      if (result !== "confirm") {
        return this.$message.info("用户取消了删除");
      }
      //正式发起删除sku的请求
    },
  },
};
</script>
<style>
.el-carousel__button {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background-color: red;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
<style lang="less" scoped>
</style>